﻿using EnumsNET;
using Kinlo.Entitys;
using Kinlo.MESDocking;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.Measure;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using MySql.Data.MySqlClient;
using NewLife.Data;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using UIWindows.Commands;
using UIWindows.GlobalStatics;
using UIWindows.Models;
using UIWindows.Models.Homes;
using UIWindows.Services;
using UIWindows.Tools.Helper;
using XingLucifer.IBase.Enums;
using static log4net.Appender.RollingFileAppender;
using static NPOI.HSSF.Util.HSSFColor;
using static System.Resources.ResXFileRef;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace UIWindows.ViewModels
{
    public class HomePageViewModel : UIWindows.Domains.ViewModelBase, XingLucifer.IBase.IViewStatus
    {
        #region 配置显示项
        public GridView ShortCicuteProductionDataView { get; set; }
        public GridView ProductionDataView { get; set; }
        public GridView AltimetryView { get; set; }
        #endregion


        #region 后称重
        public IEnumerable<ISeries> LiquidInjectionNGSeries { get; set; }
        public IEnumerable<ISeries> LeakDetectionNGSeries { get; set; }
        public IEnumerable<ISeries> AfterWeighingPassingRateSeries { get; set; }
        #endregion

        public GlobalStatics.ParameterStatic Parameter { get; set; }
        public GlobalStatics.HomeStatic Home { get; set; }
        public GlobalStatics.TemporaryStatic Temporary { get; set; }
        public SystemConfigStatic _configStatic { get; set; }
        public DisplayItemsStatic _displayItems { get; set; }

        public HomePageViewModel(GlobalStatics.ParameterStatic parameter, GlobalStatics.HomeStatic home, GlobalStatics.TemporaryStatic temporaryStatic, SystemConfigStatic configStatic, DisplayItemsStatic displayItems)
        {
            Parameter = parameter;
            Home = home;
            ShortCicuteProductionDataView = new GridView();
            AltimetryView = new GridView();

            ProductionDataView = new GridView();

            Temporary = temporaryStatic;
            _configStatic = configStatic;


            //#region 短路测试仪显示配置
            //SetDisplayItemsShortCicute();
            //SetShowShortCicute();
            //#endregion
        }


        public DelegateCommand ClockOutline => new DelegateCommand(() =>
        {

        });

        private List<DisplayItemsModel> _displayItems_ShortCircuit;
        private List<DisplayItemsModel> _displayItems_BeforeWeight;
        public void Load()
        {
            if (_displayItems == null)
            {
                _displayItems = Domains.ObjectContainer.ResolveSingleton<DisplayItemsStatic>();
            }
            if (!_displayItems.IsHomeUpdate)
            {
                return;
            }
            _displayItems.IsHomeUpdate = false;
            ProductionDataView.Columns.Clear();
            List<DisplayItemsModel> list = new List<DisplayItemsModel>(_displayItems.WeighingDisplayItems);
            list.Sort((x, y) => x.CompareTo(y));
            foreach (var item in list)
            {
                if (item.IsDisplay)
                {
                    GridViewColumn gvc = new GridViewColumn { Header = item.Description };
                    if (item.DataType == "结果")
                    {
                        FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
                        border.SetValue(Border.CornerRadiusProperty, new CornerRadius(3));
                        border.SetBinding(Border.BackgroundProperty, new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            Converter = new Tools.Converter.ResultToColorConverter()
                        });

                        FrameworkElementFactory text = new FrameworkElementFactory(typeof(TextBlock));
                        text.SetValue(TextBlock.MarginProperty, new Thickness(5, 3, 5, 3));
                        text.SetValue(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.White));
                        text.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center);
                        text.SetBinding(TextBlock.TextProperty, new Binding { Path = new System.Windows.PropertyPath(item.Name) });

                        border.AppendChild(text);
                        var dataTemplate = new DataTemplate();
                        dataTemplate.VisualTree = border;
                        gvc.CellTemplate = dataTemplate;
                    }
                    else if (item.DataType == "浮点型")
                        gvc.DisplayMemberBinding = new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            StringFormat = "F2"
                        };
                    else if (item.DataType == "时间型")
                        gvc.DisplayMemberBinding = new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            StringFormat = "yyyy-MM-dd HH:mm:ss",
                            Converter = new Tools.Converter.TimeStampToDateTimeStringConverter()
                        };
                    else
                        gvc.DisplayMemberBinding = new Binding { Path = new System.Windows.PropertyPath(item.Name) };
                    ProductionDataView.Columns.Add(gvc);
                }
            }
        }
        //public void Load()
        //{
        //    if (_displayItems == null)
        //    {
        //        _displayItems = Domains.ObjectContainer.ResolveSingleton<DisplayItemsStatic>();
        //    }
        //    if (!_displayItems.IsHomeUpdate)
        //    {
        //        return;
        //    }
        //    _displayItems.IsHomeUpdate = false;
        //    ShortCicuteProductionDataView.Columns.Clear();
        //    List<DisplayItemsModel> list = new List<DisplayItemsModel>(_displayItems.WeighingDisplayItems);
        //    list.Sort((x, y) => x.CompareTo(y));
        //    foreach (var item in list)
        //    {
        //        if (item.IsDisplay)
        //        {
        //            var gvc = new GridViewColumn()
        //            {
        //                DisplayMemberBinding = new Binding()
        //                {
        //                    Path = new System.Windows.PropertyPath(item.Name),
        //                },
        //                Header = item.Description,
        //            };
        //            switch (item.DataType)
        //            {
        //                case "浮点型":
        //                    gvc.DisplayMemberBinding.StringFormat = "F2";
        //                    break;
        //                case "时间型":
        //                    gvc.DisplayMemberBinding.StringFormat = "yyyy-MM-dd HH:mm:ss";
        //                    (gvc.DisplayMemberBinding as Binding).Converter = new Tools.Converter.TimeStampToDateTimeStringConverter();
        //                    break;
        //            }
        //            ShortCicuteProductionDataView.Columns.Add(gvc);
        //        }
        //    }

        //List<DisplayItemsModel> list = new List<DisplayItemsModel>(_displayItems.WeighingDisplayItems);
        //list.Sort((x, y) => x.CompareTo(y));
        //foreach (var item in list)
        //{
        //    if (item.IsDisplay)
        //    {
        //        GridViewColumn gvc = new GridViewColumn { Header = item.Description };
        //        if (item.DataType == "结果")
        //        {
        //            FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
        //            border.SetValue(Border.CornerRadiusProperty, new CornerRadius(3));
        //            border.SetBinding(Border.BackgroundProperty, new Binding
        //            {
        //                Path = new System.Windows.PropertyPath(item.Name),
        //                Converter = new Tools.Converter.ResultToColorConverter()
        //            });

        //            FrameworkElementFactory text = new FrameworkElementFactory(typeof(TextBlock));
        //            text.SetValue(TextBlock.MarginProperty, new Thickness(5, 3, 5, 3));
        //            text.SetValue(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.White));
        //            text.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center);
        //            text.SetBinding(TextBlock.TextProperty, new Binding { Path = new System.Windows.PropertyPath(item.Name) });

        //            border.AppendChild(text);
        //            var dataTemplate = new DataTemplate();
        //            dataTemplate.VisualTree = border;
        //            gvc.CellTemplate = dataTemplate;
        //        }
        //        else if (item.DataType == "浮点型")
        //            gvc.DisplayMemberBinding = new Binding
        //            {
        //                Path = new System.Windows.PropertyPath(item.Name),
        //                StringFormat = "F2"
        //            };
        //        else if (item.DataType == "时间型")
        //            gvc.DisplayMemberBinding = new Binding
        //            {
        //                Path = new System.Windows.PropertyPath(item.Name),
        //                StringFormat = "yyyy-MM-dd HH:mm:ss",
        //                Converter = new Tools.Converter.TimeStampToDateTimeStringConverter()
        //            };
        //        else
        //            gvc.DisplayMemberBinding = new Binding { Path = new System.Windows.PropertyPath(item.Name) };
        //        ShortCicuteProductionDataView.Columns.Add(gvc);
        //    }
        //}
        //}

        public void UnLoad()
        {

        }



        public DelegateCommand ClickMe => new DelegateCommand(() =>
        {

        });

        public DelegateCommand TestCMD => new DelegateCommand(() =>
        {
            int b = 239;
            double c = (double)b / 240;
            double a = Math.Round(c, 4, MidpointRounding.ToNegativeInfinity);
            a = a * 100;
            $"{a}".MessageDialogs(MessageLevelType.信息, false, Enums.PopUpTypes.Global);
        });
    }


}
